package cn.com.nes.site.controller.system;

import cn.com.nes.common.web.CommonResult;
import cn.com.nes.site.controller.system.vo.QueryAgentCompanyVO;
import cn.com.nes.site.entity.PageData;
import cn.com.nes.site.entity.vo.NullVO;
import cn.com.nes.site.entity.vo.system.CompanyTreeNodeVO;
import cn.com.nes.site.entity.vo.user.DeleteBusinessCompanyVo;
import cn.com.nes.site.entity.vo.user.SaveBusinessCompanyVo;
import cn.com.nes.site.service.system.company.BusinessCompanyService;
import cn.com.nes.site.service.system.company.bo.QueryAgentCompanyBO;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * 公司业务接口
 */
@Slf4j
@Controller
@RequestMapping("/v2/system/company")
public class SystemCompanyController {

    @Resource
    private BusinessCompanyService businessCompanyService;

    @ApiOperation("查询公司和部门树")
    @RequestMapping(value = "getCompanyAndDepartmentTreeNode", method = RequestMethod.GET)
    public @ResponseBody CommonResult<?> getCompanyAndDepartmentTreeNode(NullVO vo) throws Exception {
        return CommonResult.data(businessCompanyService.getCompanyAndDepartmentTreeNode(vo));
    }

    /**
     * 查询公司树
     * @param vo
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "getCompanyTreeNode", method = RequestMethod.GET)
    @ResponseBody
    public PageData getCompanyTreeNode(@Validated CompanyTreeNodeVO vo) throws Exception {
        return PageData.ok("tree", businessCompanyService.getCompanyTreeNode(vo));
    }

    /**
     * 新增、编辑业务公司
     * @param vo 保存业务公司前端入参模型
     * @return
     */
    @RequestMapping(value = "saveBusinessCompany", method = RequestMethod.POST)
    @ResponseBody
    public PageData saveBusinessCompany(@RequestBody @Validated SaveBusinessCompanyVo vo) throws Exception {
        // 当前登录用户的公司类型为00，表示创今管理员创建公司，需要传入公司类别
        if ("00".equals(vo.getUserInfo().getCompanyType()) && StrUtil.isEmpty(vo.getCompanyType())) {
            throw new Exception("公司类型必填");
        }
        return businessCompanyService.saveBusinessCompany(vo);
    }

    /**
     * 删除业务公司
     * @param vo 删除业务公司前端入参模型
     * @return
     */
    @RequestMapping(value = "deleteBusinessCompany", method = RequestMethod.POST)
    @ResponseBody
    public PageData deleteBusinessCompany(@RequestBody @Validated DeleteBusinessCompanyVo vo) throws Exception {
        return businessCompanyService.deleteBusinessCompany(vo);
    }

    /**
     * 查询厂商的代理商
     */
    @RequestMapping(value = "/queryAgentCompany", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<?> queryAgentCompany(@Validated QueryAgentCompanyVO vo) throws Exception {
        return businessCompanyService.queryAgentCompany(vo.convertToBo(QueryAgentCompanyBO.class));
    }

}
